using System;
using UnityEditor;
using UnityEngine;


/// <summary>
/// 
/// </summary>
public class EditorLayoutUtil
{
    /// <summary>
    /// UniButton 扩展
    /// </summary>
    public static void UniButton(string name, GUIStyle style, Action callback, params GUILayoutOption[] options)
    {
        bool isClick = GUILayout.Button(name, style, options);

        if (isClick)
        {
            callback?.Invoke();
        }
    }

    /// <summary>
    /// UniButton 扩展
    /// </summary>
    public static void UniButton(Texture2D texture2D, Action callback, params GUILayoutOption[] options)
    {
        bool isClick = GUILayout.Button(texture2D, options);

        if (isClick)
        {
            callback?.Invoke();
        }
    }

    /// <summary>
    /// UniButton 扩展
    /// </summary>
    public static void UniButton(GUIContent content, Action callback, params GUILayoutOption[] options)
    {
        bool isClick = GUILayout.Button(content, options);
        if (isClick)
        {
            callback?.Invoke();
        }
    }


    #region Group - Base

    /// <summary>
    /// Uni 可禁用 | 文本框
    /// </summary>
    public static string UniText(bool isEdit, string content, params GUILayoutOption[] options)
    {
        string @new;

        EditorGUI.BeginDisabledGroup(!isEdit);
        {
            @new = EditorGUILayout.TextField(content, options);
        }
        EditorGUI.EndDisabledGroup();

        return @new;
    }

    /// <summary>
    /// Uni 可禁用 | 文本区域
    /// </summary>
    public static string UniTextArea(bool isEdit, string content, params GUILayoutOption[] options)
    {
        string @new;

        EditorGUI.BeginDisabledGroup(!isEdit);
        {
            @new = EditorGUILayout.TextArea(content, options);
        }
        EditorGUI.EndDisabledGroup();

        return @new;
    }

    /// <summary>
    /// Uni 可禁用 | <see cref="UnityEngine.Object"/>
    /// </summary>
    public static T UniObject<T>(bool isEdit, UnityEngine.Object @object, params GUILayoutOption[] options) where T : UnityEngine.Object
    {
        T @new;

        EditorGUI.BeginDisabledGroup(!isEdit);
        {
            @new = EditorGUILayout.ObjectField(@object, typeof(T), true, options) as T;
        }
        EditorGUI.EndDisabledGroup();

        return @new;
    }

    /// <summary>
    /// Uni 可禁用 | 
    /// </summary>
    public static void UniButton(bool isEdit, string text, Action callback = null, params GUILayoutOption[] options)
    {
        EditorGUI.BeginDisabledGroup(!isEdit);
        {
            bool isClick = EditorGUILayout.LinkButton(text, options);
            if (isClick)
            {
                callback?.Invoke();
            }
        }
        EditorGUI.EndDisabledGroup();
    }



    #endregion

    #region Group - Combine

    /// <summary>
    /// Uni 组合 | 文本+输入区域
    /// </summary>
    public static string UniLabelTextArea(bool isEdit, string name, string content)
    {
        GUILayoutOption[] options = new GUILayoutOption[] { GUILayout.ExpandWidth(false), GUILayout.ExpandHeight(true) };
        GUILayoutOption[] optionsContent = new GUILayoutOption[] { GUILayout.ExpandWidth(true), GUILayout.ExpandWidth(true), GUILayout.MinHeight(100) };

        string @new;

        GUILayout.BeginVertical("box", options);
        {
            EditorGUILayout.LabelField(name, GUILayout.MaxWidth(100));
            @new = UniTextArea(isEdit, content, optionsContent);
        }
        GUILayout.EndVertical();

        return @new;
    }


    /// <summary>
    /// Uni 组合 | 文本+<see cref="UnityEngine.Object"/>
    /// </summary>
    public static T UniLabelObject<T>(bool isEdit, string name, UnityEngine.Object @object) where T : UnityEngine.Object
    {
        T @new;

        GUILayout.BeginHorizontal();
        {
            EditorGUILayout.LabelField(name, GUILayout.MaxWidth(100));
            @new = UniObject<T>(isEdit, @object);
        }
        GUILayout.EndHorizontal();

        return @new;
    }

    /// <summary>
    /// Uni 组合 | 文本+文本框
    /// </summary>
    public static string UniLabelText(bool isEdit, string name, string content)
    {
        GUILayoutOption[] optionText = new GUILayoutOption[] { GUILayout.ExpandWidth(true), GUILayout.Width(200) };

        string @new;

        GUILayout.BeginHorizontal();
        {
            EditorGUILayout.LabelField(name, GUILayout.MaxWidth(100));
            @new = UniText(isEdit, content, optionText);
        }
        GUILayout.EndHorizontal();

        return @new;
    }

    #endregion








    public static Editor EditorWindow;

    public static void UniWindowModel(GameObject @gameObject, Rect rect)
    {
        bool isNull = @gameObject == null;

        if (isNull)
        {
            return;
        }

        if (EditorWindow != null)
        {
            GameObject.DestroyImmediate(EditorWindow);
        }

        EditorWindow = Editor.CreateEditor(@gameObject);
        EditorWindow.OnPreviewGUI(rect, EditorStyles.whiteLabel);

        //EditorWindow.DrawPreview(rect);
        //RenderTexture rt = RenderTexture.GetTemporary(512, 512, 0);

        //// 备份原始RenderTexture
        //RenderTexture prevRT = RenderTexture.active;
        //RenderTexture.active = rt;

        //Texture2D tex = new Texture2D(512, 512, TextureFormat.ARGB32, false);
        //tex.ReadPixels(new Rect(0, 0, 512, 512), 0, 0);
        //tex.Apply();

        //tex

        //RenderTexture.ReleaseTemporary(rt);
    }
    public static void UniWindowSprite(Texture2D texture2D, Rect rect)
    {
        GUI.DrawTexture(rect, texture2D);
    }


    public static void UniDropdowm(string title, UnitMenuItem[] menuItems, FocusType focusType = FocusType.Passive, params GUILayoutOption[] options)
    {
        GUIContent titleGUI = new(title);

        bool isDropped = EditorGUILayout.DropdownButton(titleGUI, focusType, EditorStyles.toolbarDropDown);
        if (isDropped)
        {
            Rect rect = GUILayoutUtility.GetRect(titleGUI, GUIStyle.none, options);

            var menu = new GenericMenu();
            Array.ForEach(menuItems, e => menu.AddItem(e.Content, e.On, e.Func));
            menu.DropDown(rect);
        }
    }
}

public class UnitMenuItem
{
    public GUIContent Content;
    public bool On;
    public GenericMenu.MenuFunction Func;

    public UnitMenuItem(GUIContent content, bool on, GenericMenu.MenuFunction func)
    {
        Content = content; On = on; Func = func;
    }
}
